<?php

class PublicAction extends Action {
	
	
	public function _initialize()
	{
		switch (basename($_SERVER['REQUEST_URI'])) {
			case 'privacy.html':
				$pageName = 'Private'; break;
			case 'sitemap.html':
				$pageName = 'Help'; break;
			default:
				$pageName = ''; break;
		}
		$this->setFontSize();
		$this->assign("page_name", $pageName);
		$this->assign("fontSize", session('fontSize'));
		$this->assign("news_listing", $this->getRightList());
		$this->assign("changeLanguage", $this->languageBox());
		$uid = session('uid');
		if($uid){
			$count = D('Leaveword')->getNreply($uid);
		}
		//echo $count;die;
		$this->count = $count?$count:0;
	}
	
	/**
	 * 获取所在城市信息
	 * @param 经度 $longitude
	 * @param 纬度 $latitude
	 */
	public function getPosition($longitude,$latitude){
		$ak = C('ak');
		import("ORG.Util.Geocoding");
		$result = Geocoding::getAddressComponent($ak, $longitude, $latitude, Geocoding::NO_POIS);
		//var_dump($result);die;
		$position = $result['result']['addressComponent']['city'];
		$where['name']=array('like','%'.$position.'%');
		$citycode = M('city')->where($where)->field('code')->find();
		//echo $citycode;die;
		return $citycode['code'];
	}
	
	private function setFontSize()
	{
		if (isset($_GET['f']) && ($_GET['f'] =='s' || $_GET['f'] =='m' || $_GET['f'] =='l'))
		$fontSize = $_GET['f'];	
		else
		{
			if (session("fontSize") !='')
			{
				$fontSize = session("fontSize");
			}
			else
			{
				$fontSize = "s";
			}
		}
		session("fontSize", $fontSize);
	}
	
	private function getRightList()
	{
		$Model = M();			
		$Model->table('news n, news_info ni');
		$Model->where("n.news_status =1 and  n.news_id=ni.news_id and ni.news_language_id='".(int)L("lan_id")."'");		
		$Model->order("n.news_date desc limit 4");
		$news = $Model->select();	
		$newsList = '';
		$mum = 0;
		foreach ($news as $item)
		{
			$mum++;
			$newsList .= '<li> <a href="'.U('News/detail','p='.$mum).'">'.$item['news_title'].'</a></li>';
		}
		return $newsList;
	}
	
	private function languageBox()
	{
		if (L('lan_id') == 1)
			return '<a href="?l=schinese">'.L('sc').'</a>';
		else
			return '<a href="?l=tchinese">'.L('tc').'</a>';
		
	}
	
	public function getcity(){
		$this->allProvince = M('province')->select();
		$this->allCity = M('city')->select();
		/* echo "<pre>";
			var_dump($this->allProvince);die; */
	
		$this->e_allProvince = json_encode($this->allProvince);
		$this->e_allCity= json_encode($this->allCity);
	}
	
	//检验用户是否登陆
	protected function checkuid($uid,$back=''){
		if(!$uid){
			session('refer',$back);
			$this->redirect('Membership/login');
		}
	}
	
}